package com.business.activiti.service;

import com.business.activiti.mapper.auto.TaskNodeMapper;
import com.business.activiti.model.auto.TaskNode;
import com.business.activiti.model.auto.TaskNodeExample;
import com.business.system.common.base.BaseService;
import com.business.system.common.support.Convert;
import com.business.system.model.auto.TsysUser;
import com.business.system.model.custom.Tablepar;
import com.business.system.util.SnowflakeIdWorker;
import com.business.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务节点 TaskNodeService
 * @Title: TaskNodeService.java 
 * @Package com.business.activiti.service 
 * @author lujingyuan_自动生成
 * @email 674326842@qq.com
 * @date 2020-11-18 13:47:32  
 **/
@Service
public class TaskNodeService implements BaseService<TaskNode, TaskNodeExample>{
	@Autowired
	private TaskNodeMapper taskNodeMapper;

	@Autowired
	private ActivitiService activitiService;
	
      	   	      	      	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	 public PageInfo<TaskNode> list(Tablepar tablepar,String name){
	        TaskNodeExample testExample=new TaskNodeExample();
	        testExample.setOrderByClause("id ASC");
	        if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andAppealIdLike("%"+name+"%");
	        }

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<TaskNode> list= taskNodeMapper.selectByExample(testExample);
	        PageInfo<TaskNode> pageInfo = new PageInfo<TaskNode>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			TaskNodeExample example=new TaskNodeExample();
			example.createCriteria().andIdIn(lista);
			return taskNodeMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public TaskNode selectByPrimaryKey(String id) {
				
			return taskNodeMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(TaskNode record) {
		return taskNodeMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(TaskNode record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return taskNodeMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(TaskNode record, TaskNodeExample example) {
		
		return taskNodeMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(TaskNode record, TaskNodeExample example) {
		
		return taskNodeMapper.updateByExample(record, example);
	}

	@Override
	public List<TaskNode> selectByExample(TaskNodeExample example) {
		
		return taskNodeMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(TaskNodeExample example) {
		
		return taskNodeMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(TaskNodeExample example) {
		
		return taskNodeMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param taskNode
	 * @return
	 */
	public int checkNameUnique(TaskNode taskNode){
		TaskNodeExample example=new TaskNodeExample();
		example.createCriteria().andAppealIdEqualTo(taskNode.getAppealId());
		List<TaskNode> list=taskNodeMapper.selectByExample(example);
		return list.size();
	}

	/**
	 * 获取待办任务
	 * @param user
	 * @return
	 */
	public List<Map<String,String>> getTaskList(TsysUser user){
		List<Map<String,String>> mapList=new ArrayList<Map<String,String>>();
		List<Task> tasks=activitiService.getTaskListByUser(user);
		if(tasks!=null){
			for(Task task:tasks){
				Map<String,String> map=new HashMap<String,String>();
				TaskNodeExample example=new TaskNodeExample();
				example.createCriteria().andTaskIdEqualTo(task.getId());
				List<TaskNode> list=taskNodeMapper.selectByExample(example);
				//TaskNode taskNode=list.get(0);
				map.put("taskId",task.getId());
				map.put("formKey",task.getFormKey());
				map.put("taskName",task.getName());
				map.put("taskCreateTime", DateUtil.date2str(task.getCreateTime()));
				//map.put("taskNodeId",taskNode.getId());
				map.put("processInstanceId",task.getProcessInstanceId());
				mapList.add(map);
			}
		}
		return mapList;
	}


	public List<Map<String,String>> getHistoryTasks(TsysUser user){
		List<Map<String,String>> mapList=new ArrayList<Map<String,String>>();
		List<HistoricTaskInstance> tasks=activitiService.getCompletedTaskListByUser(user);
		if(tasks!=null){
			for(HistoricTaskInstance task:tasks){
				Map<String,String> map=new HashMap<String,String>();
				TaskNodeExample example=new TaskNodeExample();
				example.createCriteria().andTaskIdEqualTo(task.getId());
				List<TaskNode> list=taskNodeMapper.selectByExample(example);
				map.put("taskId",task.getId());
				map.put("formKey",task.getFormKey());

				map.put("taskName",task.getName());
				map.put("taskCreateTime", DateUtil.date2str(task.getCreateTime()));
				map.put("taskCompleteTime", DateUtil.date2str(task.getEndTime()));
				//map.put("taskNodeId",taskNode.getId());
				map.put("processInstanceId",task.getProcessInstanceId());
				if(list!=null&&list.size()>0){
					TaskNode taskNode=list.get(0);
					map.put("formInstanceKey",taskNode.getFormInstanceId());
				}
				mapList.add(map);
			}
		}
		return mapList;
	}

	/**
	 * 根据taskId查询任务节点对象
	 * @param taskId
	 * @return
	 */
    public TaskNode selectOneByTaskId(String taskId) {
		TaskNodeExample example = new TaskNodeExample();
		example.createCriteria().andTaskIdEqualTo(taskId);
		List<TaskNode> taskNodes = taskNodeMapper.selectByExample(example);
		return taskNodes.size()>0 ? taskNodes.get(0) : null;
    }
}
